package com.ruoyi.framework.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 自定义拦截器，进行rest请求的数据安全验证工作
 * @author Administrator
 *
 */
@Component
public class WebsInterceptor implements HandlerInterceptor, ApplicationContextAware {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//		WebUser user = null;
//		String userId = request.getParameter("userId");
//		if(user == null){
//			response.sendRedirect("/login");
//			return false;
//		}
		response.setContentType("text/html;charset=UTF-8");
		String userId = request.getHeader("userId");
//		String status = request.getHeader("status");
//		if(StringUtils.isNotEmpty(userId)){
//			IWebUserService iWebUserService = (IWebUserService)context.getBean(IWebUserService.class);
//			WebUser user = iWebUserService.getOne(new QueryWrapper<WebUser>().eq("id",userId).select("status"));
//			if(user != null && 1 ==(user.getStatus())){ //停用标识
//				response.getWriter().print(JSON.toJSONString(AjaxResult.warn("您当前已被禁止使用当前系统!")));
//				return false;
//			}
//		}

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	private static ApplicationContext context;
}
